<template>
	<view class="content">
		<view class='textarea-content'>
			<view class='textarea-item'>
				<!-- <input class="title" :value="title" placeholder="请输入标题" />
				<input class="title" :value="address" placeholder="请输入地址" /> -->
				<textarea class="textarea-text" v-model="text" @blur="inputText" placeholder="这一刻的想法..." />
			</view>
			<view class="upload-block">
				<view v-for="(item,index) in imgArr">
					<image :src="item" @click="previewImg(item)" class="img">
					</image>
					<view @click="closeImg(item)" style="display: inline;">
						<view class="iconfont">
							<image src="../../static/mine/840a57fd4578e66804999e3f5956b03.png" class="delete"></image>
						</view>
					</view>
				</view>
				<view @click="chooseImg" class="unload" v-if="imgArr.length<9">
					<image src="../../static/mine/shangchuan.png" class=" img"></image>
				</view>
			</view>
		</view>
		<button class="submitBtn" @click="submit">发布</button>
	</view>
</template>

<script>
	import {
		postAddUpdate
	} from '@/api/mine.js';
	export default {
		data() {
			return {
				title: '',
				text: '',
				address: '',
				imgArr: []

			}
		},
		methods: {
			//*获取文本框内容*//
			inputText: function(e) {
				this.text = e.detail.value
			},
			//上传图片
			chooseImg() {
				uni.chooseImage({
					count: 1, // 最多可以选择的图片张数，默认9
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					uplodaType: 3,
					success: res => {
						uni.uploadFile({
							url: 'https://ysz.gshwsc.com/upload?uploadType=2', //post请求的地址
							filePath: res.tempFilePaths[0],
							name: 'file',
							formData: {},
							header: {
								// Authorization: that.authtoken
							},
							success: (myres) => {
								const data = JSON.parse(myres.data);
								if (data.code == 1) {
									console.log(this.imgArr)
									if (this.imgArr.length === 0) {
										this.imgArr = this.imgArr.concat(data.imgURL)
									} else if (this.imgArr.length < 9) {
										this.imgArr = this.imgArr.concat(data.imgURL) //concat追加到数组
									}
								}

							}
						})

					}
				})
			},

			//预览图片
			previewImg(current) {
				uni.previewImage({
					current,
					urls: this.imgArr
				})
			},

			// 删除图片
			closeImg(index) {
				var that = this;
				uni.showModal({
					title: '提示',
					content: '是否删除当前图片',
					success: function(res) {
						if (res.confirm) {
							that.imgArr.splice(index, 1);
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//发布旅游圈
			getList() {
				postAddUpdate({
					title: this.title,
					content: this.text,
					address: this.address,
					imgList: this.imgArr
				}).then((res) => {
					if (res.code == 1) {
						uni.showToast({
							title: '发布成功',
							mask: true,
						})
					} else {
						uni.showToast({
							title: '加载失败',
							mask: true,
						})
					}
				}).catch((res) => {
					uni.hideLoading();
					uni.showToast({
						title: '加载失败',
						mask: true,
					})
				})
			},
			submit() {
				this.getList()
				uni.switchTab({
					url: '/pages/mine/index'
				})
			}
		},

	}
</script>

<style lang="scss" scoped>
	.content {
		padding: 30rpx;

		.title {
			border-bottom: 3rpx solid #efefef;
			height: 60rpx;
		}

		.textarea-text {
			margin-top: 20rpx;
		}

		.upload-block {
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;

			.upload {}

			.img {
				width: 230rpx;
				height: 230rpx;
				padding: 10rpx;
			}
		}

		.iconfont {
			.delete {
				width: 16px;
				height: 16px;
				margin-top: -230rpx;
				margin-left: 190rpx;
				position: absolute;
				cursor: pointer;
			}
		}

		.submitBtn {
			width: 300rpx;
			height: 60rpx;
			border-radius: 40rpx;
			font-size: 28rpx;
			background: $color-primary;
			color: #fff;
			margin-top: 20rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
</style>